package com.vivo.module.criteria;

import java.util.HashMap;
import java.util.Map;

/**
 * Criteria.Service Parameter Improvement.
 * <p>
 * Only support "and".<BR>
 * 
 * 
 * @author vivo
 * @date 2007-12-12
 * 
 * @param <T> -
 *            CriteriaMetaData Type
 */
public class Criteria<T extends CriteriaMetaData> {

	/**
	 * This Stores Data.
	 */
	private Map<String, Object> data = new HashMap<String, Object>();

	/**
	 * Constructor
	 * 
	 * @param <T> -
	 *            CriteriaMetaData Type.
	 * @param criteriaMetaData
	 * @return
	 */
	public static <T extends CriteriaMetaData> Criteria<T> newInstance(T criteriaMetaData) {
		return new Criteria<T>();
	}

	/**
	 * Add method.
	 * 
	 * @param criteriaMetaData
	 * @return
	 */
	public Criteria<T> add(T criteriaMetaData) {
		validateParameter(criteriaMetaData);
		this.data.put(criteriaMetaData.getKey(), criteriaMetaData.getValue());
		return this;
	}

	/**
	 * Add method.
	 * 
	 * @param criteriaMetaData
	 * @return
	 */
	// public Criteria<T> add(SimpleCriteriaMetaData criteriaMetaData) {
	// validateParameter(criteriaMetaData);
	// this.data.put(criteriaMetaData.getKey(), criteriaMetaData.getValue());
	// return this;
	// }
	/**
	 * Support for Pager and Order, and so on.
	 * 
	 * @param criteriaMetaData
	 * @return
	 */
	public Criteria<T> add(PublicCriteriaMetaData criteriaMetaData) {
		validateParameter(criteriaMetaData);
		this.data.put(criteriaMetaData.getKey(), criteriaMetaData.getValue());
		return this;
	}

	/**
	 * @param criteriaMetaData
	 */
	@SuppressWarnings("unchecked")
	private void validateParameter(CriteriaMetaData criteriaMetaData) {
		if (this.data.containsKey(criteriaMetaData.getKey())) {
			System.out.println("Warring : criteria already has a data with a key named \""
			        + criteriaMetaData.getKey() + "\" Criteria value is \""
			        + criteriaMetaData.getValue() + "\"");
		}
	}

	public Map<String, Object> getMap() {
		return data;
	}

	@Override
	public String toString() {
		return this.data.toString();
	}

	public static String TestApplicationID = "powazi";// TODO change this.
}
